package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.LimitWorkApproveSheet;
import com.ruoyi.huanghefd.domain.WorkApproveHeightDetail;
import com.ruoyi.huanghefd.domain.WorkApproveSheet;
import com.ruoyi.huanghefd.mapper.WorkApproveHeightDetailMapper;
import com.ruoyi.huanghefd.service.WorkApproveSheetService;
import com.ruoyi.huanghefd.mapper.WorkApproveSheetMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author gnplum
 * @description 针对表【work_approve_sheet(作业审批单)】的数据库操作Service实现
 * @createDate 2024-12-12 15:04:59
 */
@Service
public class WorkApproveSheetServiceImpl extends ServiceImpl<WorkApproveSheetMapper, WorkApproveSheet>
        implements WorkApproveSheetService {

    private static final Logger log = LoggerFactory.getLogger(WorkApproveSheetServiceImpl.class);
    @Resource
    private WorkApproveSheetMapper workApproveSheetMapper;
    @Resource
    private WorkApproveHeightDetailMapper workApproveHeightDetailMapper;

    @Override
    public AjaxResult addWorkApproveSheet(WorkApproveSheet workApproveSheet) {
        int result = 0;
        try {
            // 作业审批单 主键
            String strAsId = UUID.randomUUID().toString().replace("-", "");
            String strAsType = workApproveSheet.getAsType();

            String strAsCode = this.getGenerateOrder(strAsType);
            log.info("生成作业审批单票号：" + strAsCode);
            workApproveSheet.setAsCode(strAsCode);
            workApproveSheet.setAsId(strAsId);

            //插入 有限空间作业审批单 主表
            log.info("新增作业审批单-开始>>" + workApproveSheet);
            result = workApproveSheetMapper.insertAll(workApproveSheet);
            log.info("新增作业审批单-结束<<" + result);

            if (!CollectionUtils.isEmpty(workApproveSheet.getHeightDetails()) && "GKZY".equals(strAsType)) {
                //高空作业审批单，新增高空作业审批明细
                List<WorkApproveHeightDetail> workApproveHeightDetailList = new ArrayList<>();
                for (WorkApproveHeightDetail hds : workApproveSheet.getHeightDetails()) {
                    WorkApproveHeightDetail workApproveHeightDetail = new WorkApproveHeightDetail();
                    workApproveHeightDetail.setAsId(strAsId);
                    workApproveHeightDetail.setAsCode(strAsCode);
                    workApproveHeightDetail.setApprover(hds.getApprover());
                    workApproveHeightDetail.setSuggest(hds.getSuggest());
                    workApproveHeightDetail.setApproverSignId(hds.getApproverSignId());
                    workApproveHeightDetail.setApproverSign(hds.getApproverSign());
                    workApproveHeightDetail.setApproverTime(hds.getApproverTime());

                    workApproveHeightDetailList.add(workApproveHeightDetail);
                }
                log.info("新增高空作业审批明细-开始>>" + workApproveHeightDetailList);
                result = workApproveHeightDetailMapper.insertBatch(workApproveHeightDetailList);
                log.info("新增高空作业审批明细-结束 <<" + result);
            }

            return AjaxResult.success(strAsCode);
        } catch (Exception e) {
            return AjaxResult.error("作业审批单新增失败", e);
        }
    }

    @Override
    public AjaxResult delWorkApproveSheet(String strAsCode) {
        int result = 0;
        try {
            log.info("删除作业审批单-开始>>");
            result = workApproveSheetMapper.delByAsCode(strAsCode);
            log.info("删除作业审批单-结束<<" + result);

            //高空作业审批单，需要删除审批明细
            if (strAsCode.contains("GKZY")) {
                log.info("删除高空作业审批单-开始>>");
                result = workApproveHeightDetailMapper.delByAsCode(strAsCode);
                log.info("删除高空作业审批单-结束<<" + result);
            }
            return AjaxResult.success(strAsCode);
        } catch (Exception e) {
            return AjaxResult.error("作业审批单删除失败", e);
        }
    }

    @Override
    public AjaxResult uptWorkApproveSheet(WorkApproveSheet workApproveSheet) {
        int result = 0;
        try {
            String strAsType = workApproveSheet.getAsType();
            log.info("修改作业审批单-开始>>");
            result = workApproveSheetMapper.updateByAsCode(workApproveSheet);
            log.info("修改作业审批单-结束<<" + result);

            if (!CollectionUtils.isEmpty(workApproveSheet.getHeightDetails()) && "GKZY".equals(strAsType)) {
                //高空作业审批单，新增高空作业审批明细
                log.info("修改高空作业审批明细-开始>>");
                workApproveHeightDetailMapper.delByAsCode(workApproveSheet.getAsCode());
                result = workApproveHeightDetailMapper.insertBatch(workApproveSheet.getHeightDetails());
                log.info("修改高空作业审批明细-结束 <<" + result);
            }

            return AjaxResult.success(workApproveSheet.getAsCode());
        } catch (Exception e) {
            return AjaxResult.error("修改作业审批单失败", e);
        }
    }

    @Override
    public PageBean queryWorkApproveSheetByPage(String strAsCode, String strAsDept, String strAsState, String strUserId, String strMailType, String strAstype, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = workApproveSheetMapper.getWorkApproveSheetNum(strAsCode, strAsDept, strAsState, strUserId, strMailType, strAstype);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<WorkApproveSheet> workApproveSheetList = workApproveSheetMapper.queryWorkApproveSheetListByPage(strAsCode, strAsDept, strAsState, strUserId, strMailType, strAstype, startIndex, pageSize);
            pageBean.setList(workApproveSheetList);
            pageBean.setSuccessMsg("作业审批单查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("作业审批单查询失败！");
        }
        return pageBean;
    }

    private String getGenerateOrder(String Type) {

        log.info("获取" + Type + "票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = workApproveSheetMapper.getAsCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            throw e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




